package de.openkanban.client.ui.footer;

import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

import de.openkanban.client.datasource.BoardContactDS;
import de.openkanban.client.datasource.BoardHistoryDS;
import de.openkanban.client.util.RootPanel;

/**
 * GUI Element of the Footer.
 */
public class Footer extends VLayout {

	private static final int TITLE_HEIGHT = 16;
	private static final int STACK_HEIGHT = 160;

	private HLayout footerLayout;
	private FooterPanel footerPanel;
	private VStack boardContactStack;
	private VStack boardHistoryStack;
	private VStack userContactsStack;
	private ListGrid userContactsListGrid;
	private ListGrid boardHistoryListGrid;
	private BoardContactTileGrid boardContactsTileGrid;
	private Label noBoardContactsLabel;
	private Label noHistoryDataLabel;

	public Footer() {
		footerLayout = new HLayout();
		footerLayout.setVisible(false);
		footerLayout.setAutoHeight();

		noBoardContactsLabel = new NoBoardNameLabel(146);
		noHistoryDataLabel = new NoBoardNameLabel(146);

		// Board Contacts
		boardContactStack = new BoardContactStack(STACK_HEIGHT, TITLE_HEIGHT);
		boardContactsTileGrid = new BoardContactTileGrid();
		boardContactsTileGrid.setVisible(false);
		boardContactStack.addMember(boardContactsTileGrid);
		boardContactStack.addMember(noBoardContactsLabel);

		// User Contacts
		userContactsStack = new UserContactsStack(STACK_HEIGHT, TITLE_HEIGHT);
		userContactsListGrid = new UserContactsListGrid();
		userContactsStack.addMember(userContactsListGrid);

		// Board History
		boardHistoryStack = new BoardHistoryStack(STACK_HEIGHT, TITLE_HEIGHT);
		boardHistoryListGrid = new HistoryListGrid();
		boardHistoryListGrid.setVisible(false);
		boardHistoryStack.addMember(boardHistoryListGrid);
		boardHistoryStack.addMember(noHistoryDataLabel);

		// Adding
		footerLayout.addMember(boardContactStack);
		footerLayout.addMember(userContactsStack);
		footerLayout.addMember(boardHistoryStack);

		setBackgroundColor("#888888");
		setAutoHeight();

		footerPanel = new FooterPanel(footerLayout);

		addMember(footerPanel);
		addMember(footerLayout);
	}

	public HLayout getFooterLayout() {
		return footerLayout;
	}

	public void setFooterLayout(HLayout footerLayout) {
		this.footerLayout = footerLayout;
	}

	public VStack getBoardContactStack() {
		return boardContactStack;
	}

	public FooterPanel getFooterPanel() {
		return footerPanel;
	}

	public void showBoardContactTileGrid() {
		if (boardContactStack.contains(noBoardContactsLabel)) {
			boardContactStack.removeMember(noBoardContactsLabel);
			boardContactsTileGrid.setDataSource(BoardContactDS.getInstance(RootPanel.getCurrentBoardDto().getBoardId()
					.toString()));
			boardContactsTileGrid.fetchData();

			boardContactsTileGrid.show();
		}
	}

	public void hideBoardContactTileGrid() {
		if (!boardContactStack.contains(noBoardContactsLabel)) {
			boardContactStack.addMember(noBoardContactsLabel);
			if (boardContactsTileGrid.getDataSource() != null) {
				boardContactsTileGrid.getDataSource().destroy();
			}
			BoardContactDS.setInstanceNull();
			boardContactsTileGrid.hide();
		}
	}

	public void showBoardHistoryListGrid() {
		if (boardHistoryStack.contains(noHistoryDataLabel)) {
			boardHistoryStack.removeMember(noHistoryDataLabel);
			boardHistoryListGrid.setDataSource(BoardHistoryDS.getInstance(RootPanel.getCurrentBoardDto().getBoardId()
					.toString()));
			boardHistoryListGrid.fetchData();

			boardHistoryListGrid.show();
		}
	}

	public void hideBoardHistoryListGrid() {
		if (!boardHistoryStack.contains(noHistoryDataLabel)) {
			boardHistoryStack.addMember(noHistoryDataLabel);
			if (boardHistoryListGrid.getDataSource() != null) {
				boardHistoryListGrid.getDataSource().destroy();
			}
			BoardHistoryDS.setInstanceNull();
			boardHistoryListGrid.hide();
		}
	}

	public void updateBoardContactData() {
		boardContactsTileGrid.getDataSource().invalidateCache();
		boardContactsTileGrid.fetchData();
		boardContactsTileGrid.filterData();
		boardContactsTileGrid.redraw();
	}

	public void updateBoardHistoryData() {
		boardHistoryListGrid.getDataSource().invalidateCache();
		boardHistoryListGrid.fetchData();
		boardHistoryListGrid.filterData();
		boardHistoryListGrid.redraw();
	}

	public void updateUserContactsData() {
		userContactsListGrid.refreshFields();
		userContactsListGrid.fetchData();
		userContactsListGrid.filterData();
		userContactsListGrid.redraw();
		userContactsListGrid.refreshFields();
	}

}
